package com.tiny.tests;

import java.applet.AudioClip;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;

import com.tiny.core.game.Game;
import com.tiny.core.input.KeyboardManager;
import com.tiny.core.resources.AudioManager;
import com.tiny.core.resources.FontManager;
import com.tiny.core.resources.ImageManager;

public class ResourcesManagers extends Game {

    private ImageManager imgManager;
    private BufferedImage img;
    private BufferedImage img2;

    private FontManager fontManager;
    private Font plain;
    private Font italic;
    private Font bold;
    private Font bolditalic;

    private AudioManager audioManager;
    private AudioClip sound;

    @Override
    public void onLoad() {
        imgManager = new ImageManager();
        img = imgManager.loadImage("resources/tiny-logo-small.png");
        img2 = imgManager.loadImage("resources/tiny-logo-small.png");

        fontManager = new FontManager();
        plain = fontManager.loadFont("resources/SamDam.ttf", 20, FontManager.PLAIN);
        italic = fontManager.loadFont("resources/SamDam.ttf", 20, FontManager.ITALIC);
        bold = fontManager.loadFont("resources/SamDam.ttf", 20, FontManager.BOLD);
        bolditalic = fontManager.loadFont("resources/SamDam.ttf", 20, FontManager.BOLD_ITALIC);

        audioManager = new AudioManager();
        sound = audioManager.loadAudio("resources/sound.midi");
        sound.loop();
    }

    @Override
    public void onUnload() {
        sound.stop();
    }

    @Override
    public void onUpdate(int currentTick) {
        if (KeyboardManager.getInstance().isPressed(KeyEvent.VK_ESCAPE)) {
            terminate();
        }
    }

    @Override
    public void onRender(Graphics2D g) {
        g.setColor(Color.WHITE);
        g.drawString("Images in cache: " + imgManager.getCacheSize(), 20, 20);
        g.drawString("Fonts in cache: " + fontManager.getCacheSize(), 20, 35);

        g.drawImage(img, null, 100, 100);
        g.drawImage(img2, null, 400, 100);

        g.setFont(plain);
        g.drawString("Plain", 20, 200);

        g.setFont(italic);
        g.drawString("Italic", 20, 230);

        g.setFont(bold);
        g.drawString("Bold", 20, 260);

        g.setFont(bolditalic);
        g.drawString("Bold-Italic", 20, 290);
    }

    @Override
    public String getTitle() {
        return "Resource Manager";
    }

    public static void main(String[] args) {
        ResourcesManagers r = new ResourcesManagers();
        r.getGameConfig().setExpectedTPS(20);
        r.run();
    }
}
